import { isShowListAddBtn, isShowListDelBtn } from '@/utils/form'

export default function(options = {}) {
  const get = options.get || function () {}
  const add = options.add || function () {}
  const edit = options.edit || function () {}

  return {
    data() {
      return {
        loading: false,
        saving: false
      }
    },
    computed: {
      isDisableForm() {
        return this.loading || this.saving
      }
    },
    watch: {
      '$attrs.visible': {
        immediate: true,
        handler(visible) {
          if (visible) {
            this._afterOpen()
            if (this.$attrs.mode == 'edit') {
              this.getDetail()
              this.beforeEdit()
            }
          }
        }
      }
    },
    methods: { isShowListAddBtn, isShowListDelBtn,
      _afterOpen() { this.afterOpen() },
      afterOpen() {},
      beforeEdit() {},
      save() {
        this.beforeSave()
      },
      cancel() {
        this.closeAdder()
      },
      _startSave() {
        if (this.$attrs.mode == 'edit') {
          const form = this.startEditSave(this.form)
          edit({data: form ? form : this.form})
            .then(res => {
              this.endSave(res.data)
              // this.endEditSave()
            })
            .catch(res => {
              this.saveFailed()
            })
            .finally(res => {
              // this.finallyEditSave()
            })
        } else {
          add({data: this.form})
            .then(res => {
              console.log(res,111111111)
              this.endSave(res.data)
            })
            .catch(res => {
              this.saveFailed()
            })
            .finally(() => {
            })
        }
      },
      beforeSave() {
        this.$refs.form.validate(valid => {
          if (valid) {
            this.$confirm('确定保存吗？','保存').then(() => {
              this.saving = true
              this._startSave()
            }).catch(err => {})
          }
        })
      },
      startEditSave() {},
      endSave(data = {}) {
        this.saving = false
        this.$emit('saved', {...this.form, ...data})
        this.closeAdder()
      },
      saveFailed() {
        this.saving = false
      },
      finallySave() {},
      closeAdder() {
        this.$emit('update:visible', false)
        this.resetForm()
      },
      resetForm() {
        this.$refs.form.resetFields()
        this.form = {}
        if (this.reservedOneListItem && Object.keys(this.reservedOneListItem).length > 0) {
          // Object.keys(this.reservedOneListItem).forEach(key => {
          //   this.form[key] = new this.reservedOneListItem[key]()
          // })
          this.reserveOneListItem()
        }
      },
      getDetail() {
        this.beforeGetDetail()
        get({id: this.$attrs.id}).then(res => {
          this._afterGetDetail(res)
        })
      },
      beforeGetDetail() {
        this.loading = true
      },
      _afterGetDetail(res) {
        if (this.afterGetDetail(res) != 'do_not_process_form_data') {
          this.form = res.data
          if (this.hasReservedOneListItem(this.form)) {
            this.reserveOneListItem(this.reservedOneListItem)
          }
        }
        this.loading = false
      },
      afterGetDetail() {},
      hasReservedOneListItem() {
        return this.reservedOneListItem && Object.keys(this.reservedOneListItem).length > 0
      },
      reserveOneListItem(form) {
        Object.keys(this.reservedOneListItem).forEach(listName => {
          if (this.form[listName] && this.form[listName].length > 0)  return
          this.$set(this.form, listName, [new this.reservedOneListItem[listName]()])  // 空数组时加入一条数据
        })
      }
    }
  }
}